package trackexplorer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin{
    public static final String PLUGIN_ID  = "TrackExplorer";
    public static final String IMG_SAVE   = "image.save";
    public static final String IMG_ADD    = "image.add";
    public static final String IMG_SHOW   = "image.show";
    public static final String IMG_EXPORT = "image.export";
    public static final String IMG_OZI    = "image.ozi";
    public static final String IMG_TB     = "image.tb";
    public static final String IMG_ARROW  = "image.arrow";
    private static Activator   plugin;

    public Activator(){}

    @Override
    public void start(BundleContext context) throws Exception{
        super.start(context);
        plugin = this;
    }

    @Override
    public void stop(BundleContext context) throws Exception{
        plugin = null;
        super.stop(context);
    }

    public static Activator getDefault(){
        return plugin;
    }

    public static ImageDescriptor getImageDescriptor(String path){
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }

    @Override
    protected void initializeImageRegistry(ImageRegistry registry){
        super.initializeImageRegistry(registry);
        ImageDescriptor myImage = ImageDescriptor.createFromURL(FileLocator
            .find(Activator.getDefault().getBundle(), new Path("res/sym.png"),
                null));

        Image im = myImage.createImage();
        Rectangle b = im.getBounds();
        int w = b.width / 24;
        for(int i = 0; i < 24; i++){
            Image ret = new Image(PlatformUI.getWorkbench().getDisplay(), w,
                b.height);
            GC gc = new GC(ret);
            gc.drawImage(im, i * w, 0, w, b.height, 0, 0, w, b.height);
            gc.dispose();
            registry.put(String.valueOf(i), ret);
        }
        im.dispose();
        ImageDescriptor imd = AbstractUIPlugin.imageDescriptorFromPlugin(
            "org.eclipse.ui", "icons/full/etool16/save_edit.gif");
        registry.put(IMG_SAVE, imd);
        ImageDescriptor imd1 = AbstractUIPlugin.imageDescriptorFromPlugin(
            "org.eclipse.ui", "icons/full/obj16/add_obj.gif");
        registry.put(IMG_ADD, imd1);
        ImageDescriptor imd2 = AbstractUIPlugin.imageDescriptorFromPlugin(
            "org.eclipse.ui", "icons/full/etool16/editor_area.gif");
        registry.put(IMG_SHOW, imd2);
        ImageDescriptor imd3 = AbstractUIPlugin.imageDescriptorFromPlugin(
            "org.eclipse.ui", "icons/full/etool16/saveall_edit.gif");
        registry.put(IMG_EXPORT, imd3);
        ImageDescriptor imd4 = ImageDescriptor.createFromURL(FileLocator.find(
            Activator.getDefault().getBundle(), new Path("res/tb.gif"), null));
        registry.put(IMG_TB, imd4);
        ImageDescriptor imd5 = ImageDescriptor.createFromURL(FileLocator.find(
            Activator.getDefault().getBundle(), new Path("res/ozi.gif"), null));
        registry.put(IMG_OZI, imd5);
        ImageDescriptor imd6 = AbstractUIPlugin.imageDescriptorFromPlugin(
            "org.eclipse.ui", "icons/full/elcl16/step_current.gif");
        registry.put(IMG_ARROW, imd6);
    }

    public static ImageData rotate(ImageData srcData, int direction){
        int bytesPerPixel = srcData.bytesPerLine / srcData.width;
        int destBytesPerLine = (direction == SWT.DOWN) ? srcData.width
            * bytesPerPixel : srcData.height * bytesPerPixel;
        byte[] newData = new byte[srcData.data.length];
        int width = 0, height = 0;
        for(int srcY = 0; srcY < srcData.height; srcY++){
            for(int srcX = 0; srcX < srcData.width; srcX++){
                int destX = 0, destY = 0, destIndex = 0, srcIndex = 0;
                switch(direction){
                    case SWT.LEFT: // left 90 degrees
                        destX = srcY;
                        destY = srcData.width - srcX - 1;
                        width = srcData.height;
                        height = srcData.width;
                        break;
                    case SWT.RIGHT: // right 90 degrees
                        destX = srcData.height - srcY - 1;
                        destY = srcX;
                        width = srcData.height;
                        height = srcData.width;
                        break;
                    case SWT.DOWN: // 180 degrees
                        destX = srcData.width - srcX - 1;
                        destY = srcData.height - srcY - 1;
                        width = srcData.width;
                        height = srcData.height;
                        break;
                }
                destIndex = (destY * destBytesPerLine)
                    + (destX * bytesPerPixel);
                srcIndex = (srcY * srcData.bytesPerLine)
                    + (srcX * bytesPerPixel);
                System.arraycopy(srcData.data, srcIndex, newData, destIndex,
                    bytesPerPixel);
            }
        }
        return new ImageData(width, height, srcData.depth, srcData.palette,
            destBytesPerLine, newData);
    }

    public static void saveTBmap(File mapDir, File mapFile, Image mapImg){
        int TILE_WIDTH = 300;
        int TILE_HEIGHT = 400;
        File set = new File(mapDir, "set");
        set.mkdirs();
        ImageLoader loader = new ImageLoader();
        loader.data = new ImageData[1];
        ImageData sourceData = mapImg.getImageData();
        int width = sourceData.width;
        int height = sourceData.height;
        int depht = sourceData.depth;
        PaletteData palette = sourceData.palette;
        int x = 0, y = 0;
        while(width > x){
            while(height > y){
                int w = Math.min(TILE_WIDTH, width - x);
                int h = Math.min(TILE_HEIGHT, height - y);
                int d = depht;
                ImageData data = new ImageData(w, h, d, palette);
                Image img = new Image(null, data);
                GC gc = new GC(img);
                gc.drawImage(mapImg, -x, -y);
                gc.dispose();
                loader.data[0] = img.getImageData();
                OutputStream fos;
                try{
                    fos = new FileOutputStream(new File(set, "out_" + x + "_"
                        + y + ".png"));
                    loader.save(fos, SWT.IMAGE_PNG);
                    fos.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
                img.dispose();
                y += TILE_HEIGHT;
            }
            x += TILE_WIDTH;
            y = 0;
        }
        File newMapFile = new File(mapDir, mapFile.getName());
        try{
            newMapFile.createNewFile();
            InputStream in = new FileInputStream(mapFile);
            OutputStream out = new FileOutputStream(newMapFile);
            byte[] buf = new byte[1024];
            int len;
            while((len = in.read(buf)) > 0){
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
